import cv2
import numpy as np
from hikvision_sdk import HikCamera
from utils.face_detection import FaceDetector
from utils.fire_detection import FireDetector
from utils.alert import AlertSystem
from config import CAMERA_IP, CAMERA_USERNAME, CAMERA_PASSWORD

class MonitoringSystem:
    def __init__(self):
        self.camera = HikCamera(CAMERA_IP, CAMERA_USERNAME, CAMERA_PASSWORD)
        self.face_detector = FaceDetector()
        self.fire_detector = FireDetector()
        self.alert_system = AlertSystem()

    def start_monitoring(self):
        while True:
            # 获取摄像头画面
            frame = self.camera.get_frame()
            if frame is None:
                print("无法获取摄像头画面")
                continue

            # 进行人脸检测
            faces = self.face_detector.detect(frame)
            for face in faces:
                if self.face_detector.is_stranger(face):
                    self.alert_system.trigger_alert("检测到陌生人")

            # 进行火灾检测
            if self.fire_detector.detect(frame):
                self.alert_system.trigger_alert("检测到火灾")

            # 显示处理后的画面
            cv2.imshow('监控画面', frame)

            # 按'q'键退出
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break

        self.camera.release()
        cv2.destroyAllWindows()

if __name__ == '__main__':
    system = MonitoringSystem()
    try:
        system.start_monitoring()
    except KeyboardInterrupt:
        print("\n程序已停止")
    except Exception as e:
        print(f"发生错误: {str(e)}")